/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LearnCommandBox.java
 *
 * Created on 10-feb-2011, 19:39:23
 */
package nl.lemval.nododue.dialog;

import nl.lemval.nododue.NodoDueManager;
import nl.lemval.nododue.Options;
import nl.lemval.nododue.cmd.CommandInfo;
import nl.lemval.nododue.cmd.CommandLoader;
import nl.lemval.nododue.cmd.NodoCommand;
import nl.lemval.nododue.util.Device;
import nl.lemval.nododue.util.SerialCommunicator;
import nl.lemval.nododue.util.listeners.HexKeyListener;
import org.apache.commons.lang.StringUtils;
import org.jdesktop.application.Action;

/**
 *
 * @author Michael
 */
public class LearnCommandBox extends javax.swing.JDialog {

    private HexKeyListener hkl = new HexKeyListener(2);

    /** Creates new form LearnCommandBox */
    public LearnCommandBox(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        groupValue.setText("1");
        numValue.setText("1");
        groupValue.addKeyListener(hkl);
        numValue.addKeyListener(hkl);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JLabel titleLabel = new javax.swing.JLabel();
        infoLabel = new javax.swing.JTextArea();
        actionPanel = new javax.swing.JPanel();
        groupLabel = new javax.swing.JLabel();
        numLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        locationLabel = new javax.swing.JLabel();
        groupValue = new javax.swing.JTextField();
        numValue = new javax.swing.JTextField();
        nameValue = new javax.swing.JTextField();
        locationValue = new javax.swing.JTextField();
        buttonPanel = new javax.swing.JPanel();
        sendButton = new javax.swing.JButton();
        incrementButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(nl.lemval.nododue.NodoDueManager.class).getContext().getResourceMap(LearnCommandBox.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        titleLabel.setFont(titleLabel.getFont().deriveFont(titleLabel.getFont().getStyle() | java.awt.Font.BOLD, titleLabel.getFont().getSize()+4));
        titleLabel.setText(resourceMap.getString("titleLabel.text")); // NOI18N
        titleLabel.setName("titleLabel"); // NOI18N

        infoLabel.setBackground(javax.swing.UIManager.getDefaults().getColor("Label.background"));
        infoLabel.setColumns(20);
        infoLabel.setEditable(false);
        infoLabel.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        infoLabel.setLineWrap(true);
        infoLabel.setRows(3);
        infoLabel.setText(resourceMap.getString("infoLabel.text")); // NOI18N
        infoLabel.setWrapStyleWord(true);
        infoLabel.setBorder(null);
        infoLabel.setName("infoLabel"); // NOI18N

        actionPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        actionPanel.setName("actionPanel"); // NOI18N

        groupLabel.setText(resourceMap.getString("groupLabel.text")); // NOI18N
        groupLabel.setName("groupLabel"); // NOI18N

        numLabel.setText(resourceMap.getString("numLabel.text")); // NOI18N
        numLabel.setName("numLabel"); // NOI18N

        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        locationLabel.setText(resourceMap.getString("locationLabel.text")); // NOI18N
        locationLabel.setName("locationLabel"); // NOI18N

        groupValue.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        groupValue.setText(resourceMap.getString("groupValue.text")); // NOI18N
        groupValue.setMaximumSize(new java.awt.Dimension(27, 22));
        groupValue.setMinimumSize(new java.awt.Dimension(27, 22));
        groupValue.setName("groupValue"); // NOI18N

        numValue.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        numValue.setText(resourceMap.getString("numValue.text")); // NOI18N
        numValue.setMaximumSize(new java.awt.Dimension(27, 22));
        numValue.setMinimumSize(new java.awt.Dimension(27, 22));
        numValue.setName("numValue"); // NOI18N

        nameValue.setText(resourceMap.getString("nameValue.text")); // NOI18N
        nameValue.setName("nameValue"); // NOI18N

        locationValue.setText(resourceMap.getString("locationValue.text")); // NOI18N
        locationValue.setName("locationValue"); // NOI18N

        buttonPanel.setName("buttonPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(nl.lemval.nododue.NodoDueManager.class).getContext().getActionMap(LearnCommandBox.class, this);
        sendButton.setAction(actionMap.get("sendAction")); // NOI18N
        sendButton.setText(resourceMap.getString("sendButton.text")); // NOI18N
        sendButton.setName("sendButton"); // NOI18N

        incrementButton.setAction(actionMap.get("storeAction")); // NOI18N
        incrementButton.setText(resourceMap.getString("incrementButton.text")); // NOI18N
        incrementButton.setName("incrementButton"); // NOI18N

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(incrementButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                    .addComponent(sendButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                .addContainerGap())
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sendButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(incrementButton)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout actionPanelLayout = new javax.swing.GroupLayout(actionPanel);
        actionPanel.setLayout(actionPanelLayout);
        actionPanelLayout.setHorizontalGroup(
            actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(actionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(locationLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(groupLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(numLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(nameValue, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                        .addComponent(locationValue, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(numValue, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(groupValue, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        actionPanelLayout.setVerticalGroup(
            actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(actionPanelLayout.createSequentialGroup()
                .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, actionPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(groupLabel)
                            .addComponent(groupValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(numLabel)
                            .addComponent(numValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nameLabel)
                            .addComponent(nameValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(locationLabel)
                            .addComponent(locationValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        jButton1.setAction(actionMap.get("closeAction")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(actionPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(infoLabel, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(infoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(actionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void storeAction() {
        nameValue.setText("");

        long value = hkl.toValue(numValue.getText()) + 1;
        if (value > hkl.getMaxValue()) {
            value = 0;
            long group = hkl.toValue(groupValue.getText()) + 1;
            if (group > hkl.getMaxValue()) {
                group = 0;
            }
            groupValue.setText(Long.toString(group));
        }
        numValue.setText(Long.toString(value));
    }

    @Action
    public void sendAction() {
        if (NodoDueManager.hasConnection()) {
            CommandInfo sue = CommandLoader.get(CommandInfo.Name.SendUserEvent);
            NodoCommand cmd = new NodoCommand(sue, groupValue.getText(), numValue.getText());
            SerialCommunicator comm =
                    NodoDueManager.getApplication().getSerialCommunicator();
            comm.send(cmd);
            comm.waitCommand();

            if (StringUtils.isNotBlank(nameValue.getText())) {
                CommandInfo ue = CommandLoader.get(CommandInfo.Name.UserEvent);
// TODO: Check to read results and create device
                Device device = new Device(
                        new NodoCommand(ue, groupValue.getText(), numValue.getText()).toString());
                device.setSource("IR");
                device.setLocation(locationValue.getText());
                device.setName(nameValue.getText());
                Options.getInstance().addAppliance(device);
            }
        }
    }
//    public void startAction() {
//        if ( NodoDueManager.hasConnection() ) {
//            setCursor(new Cursor(Cursor.WAIT_CURSOR));
//            statusLabel.setText("Ophalen events voor controle...");
//            NodoMacroHandler macroHandler = new NodoMacroHandler();
//            NodoMacroList list = macroHandler.getList();
//            statusLabel.setText("Controle verzending via IR...");
//            
//            boolean found = false;
//            for (int i = 0; i < list.size(); i++) {
//                NodoMacro macro = list.get(i);
//                if ( neededMacro.equals(macro) || optionalMacro.equals(macro) ) {
//                    found = true;
//                    statusLabel.setText("Event aanwezig :-)");
//                    break;
//                }
//            }
//            
//            if ( !found ) {
//                if ( list.size() >= NodoMacroList.MAXLENGTH ) {
//                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//                    statusLabel.setText("Event ontbreekt en lijst is vol :-(");
//                    return;
//                }
//                list.add(neededMacro);
//                statusLabel.setText("Bijwerken events...");
//                macroHandler.writeList(list);
//            }
//
//            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//            statusLabel.setText("Aanleren kan beginnen!");
//            setEnabled(actionPanel, true);
//            startButton.setText("Started");
//            startButton.setEnabled(false);
//        }
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actionPanel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JLabel groupLabel;
    private javax.swing.JTextField groupValue;
    private javax.swing.JButton incrementButton;
    private javax.swing.JTextArea infoLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JTextField locationValue;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameValue;
    private javax.swing.JLabel numLabel;
    private javax.swing.JTextField numValue;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables

    @Action
    public void closeAction() {
        dispose();
    }
}
